<template>
    <div>
         <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片视图 -->
        <el-card>
            <!-- 搜索区域 -->
            <el-row :gutter="20">
                <el-col :span="10">
                <el-input placeholder="请输入内容"  clearable>
                    <el-button slot="append" icon="el-icon-search" ></el-button>
                </el-input>
                </el-col>
                <el-col :span="4">
                <el-button type="primary" @click="addGoods">添加商品</el-button>
                </el-col>
            </el-row>
            <!-- 商品信息表格 -->
            <el-table stripe border :data="goodsInfo" style="width: 100%">
                 <el-table-column type="index" label="#" width="50">
                </el-table-column>
                <el-table-column prop="goods_name" label="商品名称">
                </el-table-column>
                <el-table-column prop="goods_price" label="商品价格(元)" width="100">
                </el-table-column>
                <el-table-column prop="goods_weight" label="商品重量" width="100">
                </el-table-column>
                <el-table-column prop="add_time" label="创建时间" width="160">
                    <template slot-scope="scope">
                    {{scope.row.add_time | Ctime}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="150">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" size="mini" ></el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="delGoods(scope.row)" ></el-button>
                    </template>
                </el-table-column>
            </el-table>
             <!-- 分页列表 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[10, 20, 50, 100]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
    export default {
        data(){
            return {
                queryInfo:{
                    pagenum:1,
                    pagesize:10,
                    query:''
                },
                goodsInfo:[],
                total:0
            }
        },
        methods:{
            // 添加商品按钮的处理事件
            addGoods(){
                this.$router.push('/goods/addgoods')
            },
            //获取商品数据函数
            async getGoodsList(){
                const {data:res} = await this.$http.get('/goods',{params:this.queryInfo})
                if(res.meta.status !== 200) return this.$message.error('获取商品信息失败~')
                this.goodsInfo = res.data.goods
                this.total = res.data.total
            },
            // 页面的显示的条数发生改变时
            handleSizeChange(size){
                this.queryInfo.pagesize = size
                this.getGoodsList()
            },
            // 点击上下一页的时候触发的函数
            handleCurrentChange(num){
                this.queryInfo.pagenum = num
                this.getGoodsList()

            },
            // 删除商品函数
            async delGoods(data){
                const result = await this.$confirm('此操将删除该商品, 是否继续?', '温馨提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).catch(()=>{})
                if(result !== 'confirm') return this.$message.info('已取消删除~')
                const {data:res} = await this.$http.delete('/goods/'+data.goods_id)
                if(res.meta.status !== 200) return this.$message.error('删除商品失败~')
                this.getGoodsList()
            }
        },
        // 页面刚一创建的获取商品的数据
        created(){
            this.getGoodsList()
        },
        filters:{
            // 时间格式化
            Ctime(val){
                let time = new Date(val)
                let y = time.getFullYear()
                let M = (time.getMonth()+1+'').padStart(2,'0')
                let D = (time.getDate()+'').padStart(2,'0')
                let h = (time.getHours()+'').padStart(2,'0')
                let m = (time.getMinutes()+'').padStart(2,'0')
                let s = (time.getSeconds()+'').padStart(2,'0')
                return `${y}-${M}-${D} ${h}:${m}:${s}`
            }
        }
    }
</script>

<style lang="less" scoped>
.el-table{
    margin-top: 15px;
    margin-bottom: 20px;
}

</style>